package com.example.flutter_study;

import android.util.Log;

import androidx.annotation.NonNull;
import android.os.Handler;
import android.os.Looper;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.BasicMessageChannel;
import io.flutter.plugin.common.StringCodec;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterActivity {

    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        // Register plugins
        GeneratedPluginRegistrant.registerWith(flutterEngine);
        // Register message channel
        BasicMessageChannel messageChannel = new BasicMessageChannel(flutterEngine.getDartExecutor().getBinaryMessenger(),
                "messageChannel", StringCodec.INSTANCE);
        // Set message handler
        messageChannel.setMessageHandler(
                (message, reply) -> {
                    Log.d("message", "Android Received message: " + message);
                    reply.reply("ACK from Android!");

                    new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            messageChannel.send("Hello From Android");
                        }
                    }, 1000);
                }
        );
        //messageChannel.send("Hello From Android");
    }

}
